package ac.github.os.command

import ac.github.os.OriginSkill
import ac.github.os.api.OriginSkillAPI
import ac.github.os.internal.core.holder.plugin.CommandHolder
import ac.github.os.internal.core.store.Store
import ac.github.os.internal.core.trigger.PlayerTrigger
import org.bukkit.Bukkit
import org.bukkit.entity.Player
import taboolib.common.LifeCycle
import taboolib.common.platform.Awake
import taboolib.common.platform.ProxyCommandSender
import taboolib.common.platform.ProxyPlayer
import taboolib.common.platform.command.command
import taboolib.common.platform.function.info
import taboolib.common.platform.function.onlinePlayers
import taboolib.common5.Coerce

object Command {

    @Awake(LifeCycle.ENABLE)
    fun i() {
        command("os") {
            literal("cast") {
                dynamic {
                    suggestion<Player> { sender, context ->
                        OriginSkill.schemes.map { it.key }
                    }
                    execute<Player> { sender, context, argument ->
                        val cast = OriginSkillAPI.cast(PlayerTrigger(sender), argument, CommandHolder())
                    }
                }
            }
            literal("reload") {
                execute<ProxyCommandSender> { sender, context, argument ->
                    OriginSkill.config.reload()
                    OriginSkill.loadScheme()
                    sender.sendMessage("reload successful.")
                }
            }
            literal("test") {
                dynamic {
                    execute<ProxyCommandSender> { sender, _, argument ->
                        sender.sendMessage(Coerce.toDouble(argument).toString())
                    }
                }
            }
        }

        command("osdata", listOf("od")) {
            literal("info") {

                execute<ProxyPlayer> { sender, _, _ ->
                    giveInfo(sender, Bukkit.getPlayer(sender.name)!!)
                }

                // [player]
                dynamic(optional = true) {
                    suggestion<ProxyCommandSender> { _, _ ->
                        Bukkit.getOnlinePlayers().map { it.name }
                    }
                    execute<ProxyCommandSender> { sender, _, argument ->
                        Bukkit.getPlayer(argument)?.apply { giveInfo(sender, this) }
                    }
                }
            }
            literal("search") {

                execute<ProxyCommandSender> { sender, _, _ ->
                    giveGlobalInfo(sender, "")
                }
                // search
                dynamic(optional = true) {
                    execute<ProxyCommandSender> { sender, _, argument ->
                        giveGlobalInfo(sender, argument)
                    }
                }

            }
            literal("test") {
                execute<ProxyCommandSender> { sender, context, argument ->
                    println(Store.GLOBAL?.usable?.keys?.toList())
                }
            }
            literal("set") {


                // [key]
                dynamic(optional = true) {

                    suggestion<ProxyCommandSender> { sender, context ->
                        Store.GLOBAL?.usable?.keys?.toList()
                    }

                    dynamic {
                        execute<ProxyCommandSender> { sender, context, argument ->
                            val key = context.argument(-1)!!
                            Store.GLOBAL?.set(key, Store.Data(argument))
                            sender.sendMessage("data $key set $argument .")
                        }

                    }
                }
            }

        }

    }

    private fun giveInfo(player: ProxyCommandSender, trigger: Player) {
        val map = Store.GLOBAL?.usable?.filter { it.key.startsWith(trigger.name) } ?: mutableMapOf()
        player.sendMessage("=== ${trigger.name} data info: " + (if (map.isEmpty()) "empty" else "${map.size} count"))
        map.forEach {
            val substring = it.key.substring(trigger.name.length + 1)
            player.sendMessage("$substring = ${it.value.asString()}")
        }
    }

    private fun giveGlobalInfo(player: ProxyCommandSender, key: String) {
        val map = Store.GLOBAL?.usable?.filter { it.key.startsWith(key) || it.key == key } ?: mutableMapOf()
        player.sendMessage("=== ${if (key == "") "All" else key} data info: " + (if (map.isEmpty()) "empty" else "${map.size} count"))
        map.forEach {
            val substring = it.key
            player.sendMessage("$substring = ${it.value.asString()}")
        }
    }

}